package com.baizhiedu.rsm.service.impl;

import com.baizhiedu.rsm.dao.ClassMessageDao;
import com.baizhiedu.rsm.dao.ScheduleDao;
import com.baizhiedu.rsm.dao.StudentMessageDao;
import com.baizhiedu.rsm.entity.Student;
import com.baizhiedu.rsm.entity.StudentMessage;
import com.baizhiedu.rsm.service.ImportExportService;
import com.baizhiedu.rsm.service.StudentMessageServcie;
import com.baizhiedu.rsm.service.StudentServcie;
import com.baizhiedu.rsm.util.ExportExcelForStudent;
import com.baizhiedu.rsm.util.ParseExcelForStudent;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lby
 */
@Service
public class ImportExportServiceImpl implements ImportExportService {
    @Autowired
    private StudentMessageServcie studentMessageServcie;
    @Autowired
    private StudentMessageDao studentMessageDao;
    @Autowired
    private ScheduleDao scheduleDao;
    @Autowired
    private ClassMessageDao classMessageDao;

    @Override
    public Map<String, Object> exportStudent(Integer[] ids, HttpServletRequest req, HttpServletResponse res) {

        Map<String,Object> map = new HashMap<String,Object>(16);
        try {
            //文件名称    处理
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
            String time = sdf.format(date);
            // 不允许出现特殊字符
            String fileName = "用户"+ time + ".xls";
            //用户   查询
            List<StudentMessage> students = studentMessageDao.findStudentByIds(ids);
            // 第一步，创建一个webbook，对应一个Excel文件 核心处理对象
            HSSFWorkbook wb = new HSSFWorkbook();
            // 为当前工作簿 创建名字
            String sheetName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());

            // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet 并设置工作簿名字
            HSSFSheet sheet = wb.createSheet(sheetName);

            // 通过工具类创建表格 以及设值
            ExportExcelForStudent.createExcelOfUser(students, sheet,scheduleDao,classMessageDao);

            ExportExcelForStudent.setAttachmentFileName(req, res, fileName);
            // 从响应获取输出流  将数据写出去
            OutputStream out = res.getOutputStream();
            wb.write(out);
            // 保证数据的完整性   100k    80k
            out.flush();
            out.close();
            map.put("message", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message", false);
        }
        return map;
    }

    @Override
    public void importStudent(InputStream inputStream,Integer sessionClassId) {
        ParseExcelForStudent.parseAndImportUser(inputStream, studentMessageServcie,classMessageDao,scheduleDao,sessionClassId);
    }
}
